package example.step1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @className: ReadResolveBrand
 * @package: example.step1
 */
public class ReadResolveBrand implements Serializable {

    private int val;
    private ReadResolveBrand(int val) {
        this.val = val;
    }

    // 两个枚举值
    public static final ReadResolveBrand NIKE = new ReadResolveBrand(0);
    public static final ReadResolveBrand ADDIDAS = new ReadResolveBrand(1);

//    private Object readResolve() throws ObjectStreamException {
//        if (val == 0) {
//            return NIKE;
//        }
//        if (val == 1) {
//            return ADDIDAS;
//        }
//        return null;
//    }

    public static void print(String s) {
        System.out.println(s);
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
                "E:\\idea-workspace\\gitee\\demo\\serializable\\src\\main\\java\\objfile\\readResolvePerson.obj"))) {
            oos.writeObject(ReadResolveBrand.NIKE);
            oos.close();
        }
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                "E:\\idea-workspace\\gitee\\demo\\serializable\\src\\main\\java\\objfile\\readResolvePerson.obj"))) {
            ReadResolveBrand b = (ReadResolveBrand)ois.readObject();
            print("" + (b == ReadResolveBrand.NIKE)); // 答案显然是false
        }
    }
}
